var thmWebSQLOK = !!window.openDatabase;
var statementSupport = document.getElementById("sSupport").value;
if (thmWebSQLOK === true && statementSupport === "") {
	thmWebSQL1 = new thmWebSQLInit();
} else {
	var ifr = document.getElementById("iframeResultSQL");
	var idoc = (ifr.contentWindow || ifr.contentDocument);
	if (idoc.document) idoc=idoc.document;
	idoc.write("<div style='margin:10px;font-family:verdana;font-size:12px;'>Click <b>'Run SQL'</b> to execute the SQL statement above.</div>");
}

function showDescription() {
	document.getElementById("descriptionDIV").style.display = "block";
}

function hideDescription() {
	document.getElementById("descriptionDIV").style.display = "none";
}

function thmSQLSubmit() {
	var txt;
	if (thmWebSQLOK === true && statementSupport === "") {
		thmWebSQL1.runSQL();
	} else {
		var t=document.getElementById("textareaCodeSQL").value;
		t=t.replace(/=/gi,"thmequalsign");
		thmNoWebSQLSubmit();
	}
}

function thmWriteDBInfo() {
	var txt;
	if (thmWebSQLOK === true && statementSupport === "") {
		document.getElementById("iframeResultSQL").style.display="none";
		document.getElementById("divResultSQL").style.display="block";
		document.getElementById("restoreDBBtn").style.display="inline";
		document.getElementById("nobrowsersupport").style.display="none";
		document.getElementById("nostatementsupport").style.display="none";		
		document.getElementById("yesbrowsersupport").style.display="block";
		thmWebSQL1.myDatabase();
	} else {
		if (thmWebSQLOK === true && statementSupport === "-1") {
			document.getElementById("nostatementsupport").style.display="block";
			document.getElementById("nobrowsersupport").style.display="none";
			document.getElementById("yesbrowsersupport").style.display="none";
			document.getElementById("yesbrowsersupport2").style.display="none";
			document.getElementById("websqlexplain").style.display="none";			
			document.getElementById("descriptionDIV").style.display = "block";
		} else {
			document.getElementById("nobrowsersupport").style.display="block";
			document.getElementById("nostatementsupport").style.display="none";
			document.getElementById("yesbrowsersupport").style.display="none";
			document.getElementById("yesbrowsersupport2").style.display="none";
		}
		document.getElementById("divResultSQL").style.display="none";
		document.getElementById("iframeResultSQL").style.display="inline";
		txt = '';
		txt = txt + '<table width="100%" cclass="reference notranslate">';
		txt = txt + '<tr><th style="text-align:left;">Tablenames</th><th style="text-align:right;">Records</th></tr>';
		txt = txt + '<tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Customers\')">Customers</td>';
		txt = txt + '<td style="text-align:right;">91</td>';
		txt = txt + '</tr><tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Categories\')">Categories</td>';
		txt = txt + '<td style="text-align:right;">8</td>';
		txt = txt + '</tr><tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Employees\')">Employees</td>';
		txt = txt + '<td style="text-align:right;">10</td></tr>';
		txt = txt + '<tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'OrderDetails\')">OrderDetails</td>';
		txt = txt + '<td style="text-align:right;">518</td></tr>';
		txt = txt + '<tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Orders\')">Orders</td>';
		txt = txt + '<td style="text-align:right;">196</td></tr>';
		txt = txt + '<tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Products\')">Products</td>';
		txt = txt + '<td style="text-align:right;">77</td></tr>';
		txt = txt + '<tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Shippers\')">Shippers</td>';
		txt = txt + '<td style="text-align:right;">3</td></tr>';
		txt = txt + '<tr><td style="text-align:left;cursor:pointer;text-decoration:underline;" onclick="thmNoWebSQLSelectStar(\'Suppliers\')">Suppliers</td>';
		txt = txt + '<td style="text-align:right;">29</td></tr>';
		txt = txt + '</table>';
		document.getElementById("yourDB").innerHTML = txt;
	}
}

function thmNoWebSQLSelectStar(x) {
	var sql = "SELECT * FROM " + x + ";";
	document.getElementById("textareaCodeSQL").value = sql;
	thmNoWebSQLSubmit();
}

function thmNoWebSQLSubmit() {
	var t = document.getElementById("textareaCodeSQL").value;
	document.getElementById("code").value = t;
		
	document.getElementById("tryitform").action="trysql_view.asp?x=" + Math.random();
	validateForm();
	document.getElementById("tryitform").submit();
}

function validateForm() {
	var code=document.getElementById("code").value;
	if (code.length>5000) {
		document.getElementById("code").value="<h1>Error</h1>";
	}
}

thmWriteDBInfo();